//
//  LanguageHelper.swift
//  Alamofire
//
//  Created by 李旭峰 on 2020/7/13.
//


import Foundation
import UIKit

// 当前的语言
let CurrentLanguage: String = "CurrentLanguage"

// 用户存储的语言
let Languages: String = "AppleLanguages"

// 语言发生改变
let kLanguageChanged: String = "kLanguageChanged"

func kText(_ key: String) -> String {
    return LanguageHelper.getString(key: key)
}

class LanguageHelper: NSObject {
    
    private override init() {}
    
    // 单例
    static let shareInstance = LanguageHelper()
    
    // 存储的包
    fileprivate var bundle : Bundle?
    
    // 根据名字获取对应语言的值
    static func getString(key: String) -> String {
        let bundle = LanguageHelper.shareInstance.bundle
        let str = bundle?.localizedString(forKey: key, value: nil, table: nil)
        return str ?? "-"
    }
    
    // 初始化语言
    open func initLanguage() {
        
        // 获取当前存储的语言
        var string: String = UserDefaults.standard.string(forKey: CurrentLanguage) ?? ""
        
        if string == "" {
            // 说明用户尚未设置当前语言，那么设置当前语言为语言数组的第一个语言
            let languages = UserDefaults.standard.object(forKey: Languages) as? NSArray
            if languages?.count != 0 {
                
                if let current = languages?.object(at: 0) as? String {
                    
                    string = current
                    UserDefaults.standard.set(current, forKey: CurrentLanguage)
                    UserDefaults.standard.synchronize()
                } else {
                    ///WARN: - toast->showToast-> language_can_not_be_empty
                    //dPrintLog(kText("language_can_not_be_empty"))
//                    Toast.showToast(title: CoindomLocalizedString(key: "language_can_not_be_empty"))
                }
            } else {
                ///WARN: - toast->showToast-> can_not_find_settable_language
//                Toast.showToast(title: CoindomLocalizedString(key: "can_not_find_settable_language"))
            }
        }
        
        // 简体中文是zh-Hans-US，zh-Hans-CN
        string = string.replacingOccurrences(of: "-CN", with: "")
        string = string.replacingOccurrences(of: "-US", with: "")
        
        //        var path = Bundle.main.path(forResource:string , ofType: "lproj")
        //        if path == nil {
        // 如果指定的语言没找到文件，那么找中文简体的文件
        let path = Bundle.main.path(forResource:"zh-Hans" , ofType: "lproj")
        //        }
        bundle = Bundle(path: path!)
    }
    
    /**
     * 修改当前显示的语言
     * param: language 要设置成该语言
     */
    open func changeLanguage(language: String) {
        self.setLanguage(langeuage: language)
        // 设置语言后发送通知
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kLanguageChanged), object: nil)
    }
}

extension LanguageHelper {
    
    // 设置可用的语言
    fileprivate func setLanguage(langeuage: String) {
        
        let path = Bundle.main.path(forResource:langeuage , ofType: "lproj")
        bundle = Bundle(path: path ?? "")
        UserDefaults.standard.set(langeuage, forKey: CurrentLanguage)
        UserDefaults.standard.synchronize()
    }
}
